package workers;

import java.util.concurrent.BlockingQueue;
import java.util.logging.Level;

import model.Software;
import workerUIs.StepFrame;
import workflow.SoftwareDevelopmentWorkflow;
import workflow.step.ArchitectureDesignStep;

/**
 * ArchitectureStepWorker class.
 * 
 * @author Mark Czotter
 * @author Miklos Szennai
 * 
 */
public class ArchitectureStepWorker extends AbstractStepSwingWorker {

	protected final BlockingQueue<Software> outputUIQueue;
	private final StepFrame nextFrameUI;
	
	public ArchitectureStepWorker(final StepFrame frame, final BlockingQueue<Software> inputQueue, final BlockingQueue<Software> outputImpl, final BlockingQueue<Software> outputUI, final StepFrame nextFrameImpl, final StepFrame nextFrameUI) {
		super(frame, inputQueue, outputImpl, nextFrameImpl);
		this.outputUIQueue = outputUI;
		this.nextFrameUI = nextFrameUI;
	}

	@Override
	protected Software doInBackground() throws Exception {
		try {
			this.software = this.inputQueue.take();
			doStep(software);
			this.outputQueue.put(software);
			this.outputUIQueue.put(software);
			return software;
		} catch (Exception e) {
			logger.log(Level.SEVERE, "Exception when executing step: " + getClass().getName() + ": " + e.getMessage());
		}
		return null;
	}
	
	@Override
	protected void done() {
		super.done();
		this.nextFrameUI.getNextButton().setEnabled(true);	
		if (SoftwareDevelopmentWorkflow.automaticMode) {
			this.nextFrameUI.getNextButton().doClick();
		}
	}

	@Override
	protected Software doStep(Software software) {
		return new ArchitectureDesignStep().doStep(software);
	}

}
